import { Column, CreateDateColumn, Entity, JoinTable, ManyToMany, PrimaryGeneratedColumn, UpdateDateColumn } from "typeorm";
import { Permission } from "./permission.entity";

@Entity()
export class User {
    @PrimaryGeneratedColumn()
    id: number;

    @Column({
        length: 50
    })
    username: string;

    @Column({
        length: 50
    })
    password: string;

    @CreateDateColumn()
    createTime: Date;

    @UpdateDateColumn()
    updateTime: Date;

    //通过 @ManyToMany 声明和 Permisssion 的多对多关系。
    //多对多是需要中间表的，通过 @JoinTable 声明，指定中间表的名字。
    @ManyToMany(()=>Permission)
    @JoinTable({
        name: 'user_permission_relation'
    })
    permissions: Permission[]
}



/*查询某一用户效果展示:
 User {
    id: 1,
    username: 'newking',
    password: 'zjhQQsybzd',
    createTime: 2023-10-06T15:19:20.385Z,
    updateTime: 2023-10-06T15:19:20.385Z,
    permissions: [
      Permission {
        id: 1,
        name: 'create_aaa',
        desc: '新增 aaa',
        createTime: 2023-10-06T15:19:20.363Z,
        updateTime: 2023-10-06T15:19:20.363Z
      },
      Permission {
        id: 2,
        name: 'query_aaa',
        desc: '查询 aaa',
        createTime: 2023-10-06T15:19:20.367Z,
        updateTime: 2023-10-06T15:19:20.367Z
      },
      Permission {
        id: 3,
        name: 'delete_aaa',
        desc: '删除 aaa',
        createTime: 2023-10-06T15:19:20.367Z,
        updateTime: 2023-10-06T15:19:20.367Z
      }
    ]
  }
   */
  